﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace StyleMVVM.ViewModel
{
	public delegate void DelegateAction(object parameter);

	public delegate bool DelegateCanExecute(object parameter);

	/// <summary>
	/// A general implementation of ICommand used by ViewModels to bind to commands and events
	/// </summary>
	public sealed class DelegateCommand : ICommand
	{
		private readonly DelegateAction command;
		private readonly DelegateCanExecute canExecute;

		public DelegateCommand(DelegateAction command)
		{
			this.command = command;
		}

		public DelegateCommand(DelegateAction command, DelegateCanExecute canExecute)
		{
			this.command = command;
			this.canExecute = canExecute;
		}

		public bool CanExecute(object parameter)
		{
			if (canExecute != null)
			{
				return canExecute(parameter);
			}

			return true;
		}

		public void Execute(object parameter)
		{
			command(parameter);
		}

		public void RaiseCanExecuteChanged()
		{
			if (InternalCanExecuteChanged != null)
			{
				InternalCanExecuteChanged(this, EventArgs.Empty);
			}
		}

		event EventHandler ICommand.CanExecuteChanged
		{
			add { InternalCanExecuteChanged += value; }
			remove { InternalCanExecuteChanged -= value; }
		}

		internal EventHandler InternalCanExecuteChanged;
	}
}